package com.yifan.service;

import com.yifan.dao.BaseDao;
import com.yifan.entity.Comment;
import com.yifan.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentService {

	BaseDao dao = new BaseDao();

	// 获取列表
	public Map<String, Object> getComments(String userId, String softwareId, int page, int pageSize) {
		String sql = "select  c.*,u.realname, s.name  from t_comment c " +
				" left join t_user u on u.id = c.userId " +
				" left join t_software s on s.id = c.softwareId " +
				" where 1=1 ";
		String countsql = "select count(*) from t_comment c where 1=1 ";
		List<Object> list = new ArrayList<Object>();
		if (userId != null && userId != "") {
			sql += " and c.userId = ? ";
			countsql += " and c.userId = ? ";
			list.add(userId);
		}
		if (softwareId != null && softwareId != "") {
			sql += " and c.softwareId = ? ";
			countsql += " and c.softwareId = ? ";
			list.add(softwareId);
		}

		int count = 0;
		List<Map<String, Object>> objects = null;
		try {
			count = dao.countObject(JdbcUtils.getconn(), countsql, list);

			sql += "  limit ?,?";
			list.add((page - 1) * pageSize);
			list.add(pageSize);

			objects = dao.getObjectsRMap(JdbcUtils.getconn(), sql, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", objects);
		map.put("count", count);
		map.put("msg", "列表信息");
		map.put("code", "0");
		return map;
	}

	// 添加
	public Map<String, String> addComment(Comment comment) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "insert into t_comment (userId, softwareId, content, createDate) " +
				"values (?,?,?,?)";
		try {
			dao.addObject(JdbcUtils.getconn(), sql, new Object[] {
					comment.getUserId(),
					comment.getSoftwareId(),
					comment.getContent(),
					comment.getCreateDate()});
			map.put("msg", "ok");
			map.put("handle", "添加成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 删除
	public Map<String, String> deleteComment(int id) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "delete from t_comment where id=?";
		try {
			dao.deleteObject(JdbcUtils.getconn(), sql, new Object[] { id });
			map.put("msg", "ok");
			map.put("handle", "删除成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 批量删除
	public Map<String, String> deleteComments(String[] idarr) {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < idarr.length; i++) {
			map = deleteComment(Integer.parseInt(idarr[i]));
		}
		return map;
	}

	// 修改菜单
	public Map<String, String> updateComment(Comment comment) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "update t_comment set userId=?, softwareId=?, content=?, createDate=? where id=?";
		try {
			dao.updateObject(JdbcUtils.getconn(), sql, new Object[] {
					comment.getUserId(),
					comment.getSoftwareId(),
					comment.getContent(),
					comment.getCreateDate(),
	
					comment.getId() });
			map.put("msg", "ok");
			map.put("handle", "修改成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 获取单个
	public Map<String, Object> getComment(int id) {
		String sql = "select * from t_comment  where id=?";
		Map<String, Object> object = null;
		try {
			object = dao.getObjectRMap(JdbcUtils.getconn(), sql, new Object[] { id });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return object;
	}

	public List<Comment> getListComment(List<Object> obj) {
		List<Comment> list = new ArrayList<Comment>();
		for (int i = 0; i < obj.size(); i++) {
			Comment comment = (Comment) obj.get(i);
			list.add(comment);
		}
		return list;
	}

}
